﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class admin_AdminOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDataTable();
            bindExamAllNameList();
        }
    }
    private void bindDataTable()
    {
        DataTable dt = Exam.getAllOrderInfos();
        bindTable(dt);
    }
    private void bindExamAllNameList()
    {
        DataTable dt = Exam.ExamTime_GetTopList();
        if (dt.Rows.Count > 0)
        {
            if (dt.Columns.Contains("EmTimeValue"))
                dt.Columns.Remove("EmTimeValue");
            dt.Columns.Add("EmTimeValue");
            foreach (DataRow typeName in dt.Rows)
            {
                typeName["EmTimeValue"] = typeName["EmID"].ToString() + "-" + typeName["EmYear"].ToString() + "-" + typeName["EmNum"].ToString();


                //ListItem liType = new ListItem(typeName["EmAllName"].ToString());
                //List_ExamAllName.Items.Add(liType);
            }
            DataRow dr = dt.NewRow();
            dr["EmTimeValue"] = "0-0-0";
            dr["EmAllName"] = "全部";
            dt.Rows.InsertAt(dr,0);
            BSUtility.BindListControl(ddlExamAllName, dt, "EmAllName", "EmTimeValue");
        }
    }
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        bindDataTable();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string orderState = List_OrderState.SelectedValue.Trim();
        string examAllName = ddlExamAllName.SelectedValue.ToString();
        DataTable dt = Exam.getAllOrderInfos();
        if (dt.Rows.Count > 0)
        {
            string[] strs = examAllName.Split('-');
            string applyState = ddlExamAllName.SelectedValue.ToString();
            if (dt.Columns.Contains("OrderStateCHN"))
                dt.Columns.Remove("OrderStateCHN");
            dt.Columns.Add("OrderStateCHN");
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["OrderState"].ToString().Trim() == "0")
                {
                    dr["OrderStateCHN"] = "未付款";
                }
                else if (dr["OrderState"].ToString().Trim() == "5")
                {
                    dr["OrderStateCHN"] = "付款中";
                }
                else
                {
                    dr["OrderStateCHN"] = "已付款";
                }
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(" 1 = 1 ");
            if (strs[0] != "0" && strs[1] != "0" && strs[2] != "0")
            {
                sb.Append(" And EmID='" + strs[0].Trim() + "'");
                sb.Append(" And EmYear='" + strs[1].Trim() + "'");
                sb.Append(" And EmNum='" + strs[2].Trim() + "'");
            }
            if (orderState != "-1")
            {
                sb.Append(" And OrderState='" + orderState + "'");
            }
            //备注：输入订单号时候会匹配身份证号里面的数字，然后还有查找订单号相同的数据
            if (txtStudentId.Text.Trim() != "")
            {
                sb.Append(" And( StudIdcard like '%" + txtStudentId.Text.Trim() + "%' Or OrderID = " + txtStudentId.Text.Trim() + "Or StudID like '%"+ txtStudentId.Text.Trim() +"%')");
            }
            dt.DefaultView.RowFilter = sb.ToString();
            gv.DataSource = dt.DefaultView;
            gv.DataBind();
        }
        else
        {
            gv.Visible = false;
        }

    }
    private void bindTable(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            if (dt.Columns.Contains("OrderStateCHN"))
                dt.Columns.Remove("OrderStateCHN");
            dt.Columns.Add("OrderStateCHN");
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["OrderState"].ToString().Trim() == "0")
                {
                    dr["OrderStateCHN"] = "未付款";
                }
                else if (dr["OrderState"].ToString().Trim() == "5")
                {
                    dr["OrderStateCHN"] = "付款中";
                }
                else
                {
                    dr["OrderStateCHN"] = "已付款";
                }
            }
            gv.DataSource = dt;
            gv.DataBind();
        }
    }
}